Imports System
Imports System.Configuration
Imports System.Web.Configuration

Public Class ConfigManager

    'obtiene la seccion de configuracion de archivo
    Private Shared Function getconfiguration() As Configuration
        Dim path As String 'path del archivo de configuracion
        Dim config As Configuration 'objeto de configuracion
        'verificamos el entorno
        If System.Web.HttpContext.Current Is Nothing Then
            'windows 
            path = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName)
            'abrimos configuraciones windows
            config = ConfigurationManager.OpenExeConfiguration(path)
        Else
            'web
            path = System.Web.HttpContext.Current.Request.ApplicationPath
            'abrimos la configuracion web
            config = WebConfigurationManager.OpenWebConfiguration(path)
        End If
        Return config
    End Function

    'lee un valor de la configuracion
    Public Shared Function getvalue(ByVal key As String) As String
        Dim config As Configuration
        Dim value As String
        Try
            config = getconfiguration()
            Try
                'obtengo el valor
                value = config.AppSettings.Settings(key).Value

            Catch ex As Exception
                'si no existe creo una excepcion
                Throw (New KeyNotFoundException(String.Format("Error: la clave '{0}' no existe en el archivo de configuracion", key), ex))
            End Try
            Return value
        Finally
            config = Nothing
        End Try

    End Function

End Class
